package com.erp.erp_servers.Contract_Info.Imp;

import com.erp.enums.OperationStatusEnum;
import com.erp.enums.SequenceEnum;
import com.erp.erp_dao.Contract_Info.*;
import com.erp.erp_dao.nextId.NextIdDao;
import com.erp.erp_entitys.Contract_Info.ContractInfo;
import com.erp.erp_entitys.Contract_Info.ContractProductCostInfo;
import com.erp.erp_entitys.Contract_Info.ContractProductInfo;
import com.erp.erp_entitys.Contract_Info.ContractProductPartsInfo;
import com.erp.erp_servers.Contract_Info.ContractInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 外销合同主表(ContractInfo)表服务实现类
 *
 * @author makejava
 * @since 2020-10-11 18:44:42
 */
@Transactional(readOnly = true)
@Service("contractInfoService")
public class ContractInfoServiceImpl implements ContractInfoService {
    @Resource
    private ContractInfoDao contractInfoDao;
    @Resource
    private ContractInfoexDao contractInfoexDao;
    @Resource
    private ContractProductInfoDao productInfoDao;
    @Resource
    private ContractProductAnnexDao contractProductAnnexDao;
    @Resource
    private ContractProductCostInfoDao costInfoDao;
    @Resource
    private ContractProductPartsInfoDAO partsInfoDAO;
    @Resource
    private NextIdDao nextIdDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ContractInfo queryById(Long id) {
        ContractInfo contractInfo = this.contractInfoDao.queryById(id);
        return contractInfo;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<ContractInfo> queryAllByLimit(int offset, int limit) {
        return this.contractInfoDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param contractInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ContractInfo insert(ContractInfo contractInfo) {
        contractInfo.setId(nextIdDao.selectNextId(SequenceEnum.CONTRACTINFO.getValue()));
        contractInfo.setStatus(String.valueOf(OperationStatusEnum.DRAFT_BOX.getId()));
        contractInfo.setCreatetime(new Date());
        contractInfo.setEdittime(new Date());
       /* contractInfo.getProductInfos().forEach(product ->{
            product.setContractid(contractInfo.getId());
            this.productInfoDao.insert(product);
        });
        contractInfo.getCosts().forEach(cost ->{
            cost.setContractid(contractInfo.getId());
            this.costInfoDao.insert(cost);
        });
        contractInfo.getPartsInfos().forEach(parts ->{
            parts.setContractid(contractInfo.getId());
            this.partsInfoDAO.insert(parts);
        });*/
        this.contractInfoDao.insert(contractInfo);
        return contractInfo;
    }

    /**
     * 修改数据
     *
     * @param contractInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ContractInfo update(ContractInfo contractInfo) {
        contractInfo.setEdittime(new Date());
       /* contractInfo.getProductInfos().forEach(cost ->{
            if(cost.getProductid() != null){
                this.productInfoDao.update(cost);
            }else {
                cost.setContractid(contractInfo.getId());
                this.productInfoDao.insert(cost);
            }
        });
        contractInfo.getCosts().forEach(cost ->{
            if(cost.getId() != null){
                this.costInfoDao.update(cost);
            }else {
                cost.setContractid(contractInfo.getId());
                this.costInfoDao.insert(cost);
            }
        });
        contractInfo.getPartsInfos().forEach(parts ->{
            if(parts.getPartsid() != null){
                this.partsInfoDAO.update(parts);
            }else {
                parts.setContractid(contractInfo.getId());
                this.partsInfoDAO.insert(parts);
            }
        });*/
        this.contractInfoDao.update(contractInfo);
        return this.queryById(contractInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        int i = contractInfoexDao.deleteById(id);
        int j = productInfoDao.deleteByContractID(id);
        int k = contractProductAnnexDao.deleteByContractId(id.longValue());
        return this.contractInfoDao.deleteById(id) > 0;
    }

    /**
     * 分页查询
     *
     * @param contractInfo
     * @return 是否成功
     */
    @Override
    public PageInfo<ContractInfo> selectContractInfoByPage(ContractInfo contractInfo) {
        List<ContractInfo> contractInfos = contractInfoDao.selectContractInfoByPage(contractInfo);
        PageInfo pageInfo = new PageInfo(contractInfos);
        pageInfo.setList(contractInfos);
        return pageInfo;
    }

    @Override
    public PageInfo<ContractProductPartsInfo> selectPartsByPage(ContractProductPartsInfo record) {
        List<ContractProductPartsInfo> quotedInfos = partsInfoDAO.queryAll(record);
        PageInfo pageInfo = new PageInfo(quotedInfos);
        pageInfo.setList(quotedInfos);
        return pageInfo;
    }

    @Override
    public PageInfo<ContractProductCostInfo> selectCostByPage(ContractProductCostInfo record) {
        List<ContractProductCostInfo> quotedInfos = costInfoDao.queryAll(record);
        PageInfo pageInfo = new PageInfo(quotedInfos);
        pageInfo.setList(quotedInfos);
        return pageInfo;
    }

    @Override
    public PageInfo<ContractProductInfo> selectProductsByPage(ContractProductInfo record) {
        List<ContractProductInfo> quotedInfos = productInfoDao.queryAllByContractID(record.getContractid());
        PageInfo pageInfo = new PageInfo(quotedInfos);
        pageInfo.setList(quotedInfos);
        return pageInfo;
    }

    @Override
    @Transactional
    public Boolean deleteProduct(Long id) {
        ContractProductInfo productInfo = productInfoDao.selectById(id);
        partsInfoDAO.deleteByGoodsId(productInfo.getContractproductid());
        return this.productInfoDao.deleteById(id) > 0;
    }

    @Override
    public Boolean deletePart(Long id) {
        return this.partsInfoDAO.deleteById(id) > 0;
    }

    @Override
    public Boolean deleteCost(Long id) {
        return this.costInfoDao.deleteById(id) > 0;
    }
}